﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_default_view_sub_threadgroup : BoardSubPartBase
{
	private BoardThreadCollection CurrentGroupThreadCollection;

	protected void Page_Init(Object sender, EventArgs e)
	{
		this.CurrentGroupThreadCollection = (BoardThreadCollection)this.Page.Items["CurrentGroupThreadCollection"];
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if(!Page.IsPostBack)
			this.ThreadGroupRepeater_Bind();
	}

	protected void ThreadGroupRepeater_Bind()
	{
		if (this.CurrentGroupThreadCollection != null && this.CurrentGroupThreadCollection.Count > 1)
		{
			ThreadGroupRepeater.DataSource = this.CurrentGroupThreadCollection;
			ThreadGroupRepeater.DataBind();
		}
	}

	protected void ThreadRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			BoardThread currentThread = (BoardThread)e.Item.DataItem;

			#region 답글 이미지
			Image replyImage = (Image)e.Item.FindControl("ReplyImage");
			if (currentThread.ThreadDepth > 0)
			{
				replyImage.ImageUrl = GetLocalResourceObject("Symbol_Reply_ImageUrl").ToString();
				replyImage.AlternateText = GetLocalResourceObject("Symbol_Reply_Text").ToString();
			}
			else
			{
				replyImage.Visible = false;
			}
			#endregion

			#region 링크
			HyperLink linkWebControl = (HyperLink)e.Item.FindControl("ThreadHyperLink");
			linkWebControl.Text = currentThread.Title;
			linkWebControl.NavigateUrl = currentThread.PageUrl;

			if (currentThread.IsSecret)
			{
				//작성자 검사
				//관리자 또는 시샵 또는 글작성자인지 확인
				if (!(base.CurrentUserInfo.IsAdministrator ||
					base.CurrentUserInfo.IsSysop(base.CurrentBoard) ||
					(currentThread.ProviderUserKey == base.CurrentUserInfo.ProviderUserKey)))
				{
					linkWebControl.NavigateUrl = "";
				}
			}

			if (!String.IsNullOrEmpty(BoardParameters.SearchText))
				linkWebControl.Text = linkWebControl.Text.Replace(BoardParameters.SearchText,
					String.Format(VUtility.GetVWebGlobalResource("General_SearchTextFormat"), BoardParameters.SearchText));
			#endregion

			#region 카테고리
			PlaceHolder categoryPlaceHolder = (PlaceHolder)e.Item.FindControl("CategoryPlaceHolder");

			if (!String.IsNullOrEmpty(currentThread.CategoryName))
			{
				BoardCategory currentCategory = base.CurrentBoard.GetCategoryByID(currentThread.CategoryID);

				if (currentCategory.HasIconImage)
				{
					Image categoryImage = new Image();
					categoryImage.ImageUrl = currentCategory.IconImageUrl;
					categoryImage.AlternateText = currentCategory.Name;
					categoryPlaceHolder.Controls.Add(categoryImage);
				}
				else
				{
					Label categoryNameLabel = new Label();
					categoryNameLabel.Text = String.Format(GetLocalResourceObject("ThreadList_CategoryFormat").ToString(), currentCategory.Name, currentCategory.CategoryID);
					categoryNameLabel.CssClass = "Category_" + currentCategory.CategoryID;
					categoryPlaceHolder.Controls.Add(categoryNameLabel);
				}
			}
			#endregion

			#region 글강조
			Image stressImageWebControl = (Image)e.Item.FindControl("StressImage");
			stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Stress_Normal_ImageUrl").ToString();
			stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Stress_Normal_Text").ToString();

			if (BoardParameters.ThreadID == currentThread.ThreadID)
			{
				//현재글
				stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Current_ImageUrl").ToString();
				stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Current_Text").ToString();
			}
			else if (currentThread.IsAnno)
			{
				//공지사항
				stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Stress_Anno_ImageUrl").ToString();
				stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Stress_Anno_Text").ToString();
			}
			else
			{
				//새글강조
				if (base.CurrentBoard.UseStressNew)
				{
					DateTime _now = DateTime.Now;
					TimeSpan _timeSpan = _now - currentThread.RegistrationDate;

					if (_timeSpan.TotalMinutes < 1440)
					{
						stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Stress_New_ImageUrl").ToString();
						stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Stress_New_Text").ToString();
					}
				}
				//오늘글강조
				if (base.CurrentBoard.UseStressToday)
				{
					if (DateTime.Now.ToShortDateString() == currentThread.RegistrationDate.ToShortDateString())
					{
						stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Stress_Today_ImageUrl").ToString();
						stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Stress_Today_Text").ToString();
					}
				}
				//인기글강조
				if (base.CurrentBoard.UseStressHot)
				{
					if (currentThread.ReadCount > base.CurrentBoard.HotCount)
					{
						stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Stress_Hot_ImageUrl").ToString();
						stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Stress_Hot_Text").ToString();
					}
				}
				//비밀글
				if (currentThread.IsSecret)
				{
					stressImageWebControl.ImageUrl = GetLocalResourceObject("Symbol_Stress_Secret_ImageUrl").ToString();
					stressImageWebControl.AlternateText = GetLocalResourceObject("Symbol_Stress_Secret_Text").ToString();
				}
			}
			#endregion

			#region 옵션
			PlaceHolder optionIconsPlaceHolder = (PlaceHolder)e.Item.FindControl("OptionIconPlaceHolder");
			Image optionImage = null;
			Literal empty = null;

			#region 관련 링크
			if (currentThread.Links != null)
			{
				optionImage = new Image();
				optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Link_ImageUrl").ToString();
				optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Link_Text").ToString();
				optionIconsPlaceHolder.Controls.Add(optionImage);
				empty = new Literal();
				empty.Text = "&nbsp;";
				optionIconsPlaceHolder.Controls.Add(empty);
			}
			#endregion

			if (currentThread.HasFile)
			{
				AttachmentFileInfoCollection attachmentFileInfoCollection = base.CurrentBoard.GetAttachmentFileManager().GetAttachmentFileInfoCollection(currentThread.ThreadID);

				#region 썸네일
				PlaceHolder thumbnailPlaceHolder = (PlaceHolder)e.Item.FindControl("ThumbnailPlaceHolder");
				thumbnailPlaceHolder.Visible = false;

				if (!currentThread.IsSecret && !currentThread.IsBlocked && base.CurrentUserFileDownload)
				{
					if (attachmentFileInfoCollection != null)
					{
						foreach (AttachmentFileInfo file in attachmentFileInfoCollection)
						{
							if (file.UploadFile.ContentType.IndexOf("image") > -1)
							{
								Image ThumbnailImage = new Image();
								ThumbnailImage.ImageUrl = BoardUtil.GetAttachmentThumbnailUrl(base.CurrentBoard.ComponentID, file.FileID);
								ThumbnailImage.AlternateText = "ThumbnailImage";

								thumbnailPlaceHolder.Controls.Add(ThumbnailImage);
								thumbnailPlaceHolder.Visible = true;
								break;
							}
						}
					}
				}
				#endregion

				#region 첨부파일
				if (attachmentFileInfoCollection != null)
				{
					Collection<String> contentTypes = new Collection<string>();

					foreach (AttachmentFileInfo fileInfo in attachmentFileInfoCollection)
					{
						if (fileInfo != null)
						{
							if (fileInfo.UploadFile.ContentType.IndexOf("image") > -1)
							{
								if (contentTypes.IndexOf("image") < 0)
									contentTypes.Add("image");
							}
							else if (fileInfo.UploadFile.ContentType.IndexOf("audio") > -1)
							{
								if (contentTypes.IndexOf("audio") < 0)
									contentTypes.Add("audio");
							}
							else if (fileInfo.UploadFile.ContentType.IndexOf("video") > -1)
							{
								if (contentTypes.IndexOf("video") < 0)
									contentTypes.Add("video");
							}
							else if (fileInfo.UploadFile.ContentType.IndexOf("flash") > -1)
							{
								if (contentTypes.IndexOf("flash") < 0)
									contentTypes.Add("flash");
							}
							else
							{
								if (contentTypes.IndexOf("etc") < 0)
									contentTypes.Add("etc");
							}
						}
					}

					foreach (String contentType in contentTypes)
					{
						switch (contentType)
						{
							case "image":
								optionImage = new Image();
								optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Image_ImageUrl").ToString();
								optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Image_Text").ToString();
								optionIconsPlaceHolder.Controls.Add(optionImage);
								empty = new Literal();
								empty.Text = "&nbsp;";
								optionIconsPlaceHolder.Controls.Add(empty);
								break;
							case "audio":
								optionImage = new Image();
								optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Audio_ImageUrl").ToString();
								optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Audio_Text").ToString();
								optionIconsPlaceHolder.Controls.Add(optionImage);
								empty = new Literal();
								empty.Text = "&nbsp;";
								optionIconsPlaceHolder.Controls.Add(empty);
								break;
							case "video":
								optionImage = new Image();
								optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Video_ImageUrl").ToString();
								optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Video_Text").ToString();
								optionIconsPlaceHolder.Controls.Add(optionImage);
								empty = new Literal();
								empty.Text = "&nbsp;";
								optionIconsPlaceHolder.Controls.Add(empty);
								break;
							case "flash":
								optionImage = new Image();
								optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Flash_ImageUrl").ToString();
								optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Flash_Text").ToString();
								optionIconsPlaceHolder.Controls.Add(optionImage);
								empty = new Literal();
								empty.Text = "&nbsp;";
								optionIconsPlaceHolder.Controls.Add(empty);
								break;
							case "etc":
								optionImage = new Image();
								optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_File_ImageUrl").ToString();
								optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_File_Text").ToString();
								optionIconsPlaceHolder.Controls.Add(optionImage);
								empty = new Literal();
								empty.Text = "&nbsp;";
								optionIconsPlaceHolder.Controls.Add(empty);
								break;
						}
					}
				}
				#endregion
			}

			#region 투표
			if (currentThread.HasVote)
			{
				optionImage = new Image();
				optionImage.ImageUrl = GetLocalResourceObject("Symbol_Att_Vote_ImageUrl").ToString();
				optionImage.AlternateText = GetLocalResourceObject("Symbol_Att_Vote_Text").ToString();
				optionIconsPlaceHolder.Controls.Add(optionImage);
			}
			#endregion
			#endregion

			#region 조회수, 평점
			((Literal)e.Item.FindControl("DetailLiteral")).Text = String.Format(GetLocalResourceObject("DetailFormat").ToString(),
				ResolveClientUrl(GetLocalResourceObject(String.Format("Rating_{0}_Small", (Int32)Math.Round(currentThread.Rating, 0))).ToString()),
				currentThread.Rating
			);
			#endregion
		}
	}
}
